// Após a inicialização do documento.
$(function() {

	/*$(".produto-lancamento").each(function(){
		$(this).text("Lançamento");
	});*/
	
    //$('.bread-crumb li a:contains("Daslu")').html("Store");
    $('.bread-crumb li a:contains("Daslu"), .bread-crumb li a:contains("Store")').html('<img style="position: relative; top: 6px;" src="/arquivos/img_bread_logo.png" alt="Daslu" />');
	
    $('#navigation li').find('li:last:not(li li li)').addClass('notchild');
    
	$('#navigation li').mouseenter(function(){
    
        $(this).find('ul:first').show();
        
    });
	
    $('#navigation li').mouseleave(function(){
    
        $(this).find('ul').hide();
        
    });
    
    // Substituindo as fontes do menu por cuffons.
	Cufon.replace('#navigation a:not("ul ul ul a")', {
        fontFamily: 'Proxima Nova Rg',
        hover: { color: '#fff' } 
    });
	
	Cufon.replace('.cufon', {
        fontFamily: 'Proxima Nova Rg'
    });
	
    // Substituindo as fontes do menu inferior por cuffons.
    Cufon.replace('#footer-navigation ul li a', {
        fontFamily: 'Proxima Nova Rg',
        hover: { color: '#fff' } 
    });


    Cufon.replace('#heading .bread-crumb ul li, #heading h1', {
        fontFamily: 'Proxima Nova Rg'
    });
    
    Cufon.replace('.breadcrumb ul li, .bread-crumb ul li', {
        fontFamily: 'Proxima Nova Rg'
    });
    
	Cufon.replace('.prateleira li a', {
        fontFamily: 'Proxima Nova Rg'
    });
	Cufon.replace('.giftList-wrapper span.name', {
        fontFamily: 'Proxima Nova Rg'
    });
	
	

	
	
	
	// Legado
	Cufon.replace('body .content h2', {
        fontFamily: 'Proxima Nova Rg'
    });
	Cufon.replace('body.login .content h4', {
        fontFamily: 'Proxima Nova Rg'
    });
	Cufon.replace('body.login .sign-in label', {
        fontFamily: 'Proxima Nova Rg'
    });
	
    
	//carrinho
	Cufon.replace('table.carrinhoTable thead th, table.carrinhoTable thead td', {
        fontFamily: 'Proxima Nova Rg'
    });
	Cufon.replace('.endereco .lista-endereco legend span', {
        fontFamily: 'Proxima Nova Rg'
    });
	Cufon.replace('body.carrinho .carrinho-vazio h3, body.checkout .cadastro-endereco h3', {
        fontFamily: 'Proxima Nova Rg'
    });
	Cufon.replace('body.minha-conta #orderInfoProductList th', {
        fontFamily: 'Proxima Nova Rg'
    });
	
	Cufon.replace('.descricao-compra thead th', {
        fontFamily: 'Proxima Nova Rg'
    });
    
	
    
	if($.browser.msie && $.browser.version=="6.0") {
	
		// Fixing the layout height bug
		$(window).resize(function() {

			if( $('#general').height() < $(window).height() ) {  
			
				$('#general').height( $(window).height() );
				$('#background').height( $('#general').height() );
				$('#header,#main,#footer').show();
			
			}else {
				$('#background').height( $('#general').height() );
				$('#header,#navigation,,#main,#footer').show();
			}
		});
		
		if( $('#general').height() < $(window).height() ) {  
			
			$('#general').height( $(window).height() );
			$('#background').height( $('#general').height() );
			$('#header,#main,#footer').show();
		
		}else {
			$('#background').height( $('#general').height() );
			$('#header,#navigation,,#main,#footer').show();
		}
		
	
	}
	
	
	// Eventos da Interface
	$('#navigation li').mouseenter(function (){
		$(this).find('ul:first').show();
	});
	$('#navigation li').mouseleave(function (){
		$(this).find('ul').hide();
	});
	
	$('.prateleira li').live('mouseenter',function() {
		$(this).find('span:not(.soldmini,.hidden)').show();
	});
	$('.prateleira li').live('mouseleave', function() {
		$(this).find('span:not(.soldmini,.hidden)').hide();
	});
	
	
		
	
	$(".searchWrapper:first").appendTo("#searchbar");
	Cufon.replace('#searchbar', {
        fontFamily: 'Proxima Nova Rg'
    });
		// Link de retorno para o login
	if($("#cart").find("p.loginmsg").length > 0){
		var originalURL = $("#cart p.loginmsg a").attr("href");
		originalURL = originalURL + "?ReturnUrl=" + encodeURI(location.pathname) + encodeURI(location.search);
		$("#cart p.loginmsg a").attr("href", originalURL);
	}
	
	// Link de retorno para o logout
	// Isso não funciona por causa do BACK-END!!!
	
	var logoutReturnLink = setTimeout(function(){
		if($("#lnkLogOut").length > 0){
			var originalURL = $("#lnkLogOut").attr("href");
			originalURL = originalURL + "?ReturnUrl=" + encodeURI(location.pathname) + encodeURI(location.search);
			$("#lnkLogOut").attr("href", originalURL);
		}
	}, 2000);
	
	$(document).ajaxComplete(function(event,request, settings){
	
		$("#cart .giftlisttypecountitems").html( '(' + $("#cart .giftlisttypecountitems").html().replace("(","").replace(")","").replace(" ","").replace("\n","") + ')' );

		if ( $('.amount-items a').size() == 0 ) {
		
			$('.amount-items').append('<a href="/cart/"> </a>');
		
		}
	
		
		if ( $(".cartInfoWrapper .amount-items-em").size() > 0 ) {
	
		var qtd = $(".cartInfoWrapper .amount-items-em").html();
		var qtd = qtd.replace("(","");
		var qtd = qtd.replace(")","");
		var qtd = qtd.replace(" ","");
		//var cartItens = $(".carrinho-cheio").text().match(/\d+/gi);
		//if(cartItens != null) qtd = cartItens;

		
		$(".cartInfoWrapper .amount-items-em").empty().html("(" + qtd + ")");
		//$(".cartInfoWrapper .amount-items-em").text( $(".cartInfoWrapper .amount-items-em").text().replace(/\d+/,"(" + qtd + ")") );
		
		$('.amount-items').find('.amount-items-em').appendTo('.amount-items a');
		
		}
		
		$(".loginmsg").prependTo("#cart");
		
		//substitui o texto "clique aqui" por "Não é você?" no header
		var cliqueAqui = $("#pnlNaoSeguro em a").clone();
		var position = $(".wishlishbutton").offset();
		$("#pnlNaoSeguro em").text("");
		$("#pnlNaoSeguro em").append(cliqueAqui);
		$("#pnlNaoSeguro em a").text( $("#pnlNaoSeguro em a").text().replace("clique aqui","Não é Você?") );
		
		if( $("#cart .loginmsg a:contains('Login')").size() > 0 ){
			
			offsettext = $('#cart .loginmsg').offset();
			offseticon = $('#cart .cartInfoWrapper .amount-items .amount-items-em').offset();
			if(offseticon != null){
				$('#cart .loginmsg').offset({ left: offseticon.left });
			}
				
		}
		
		//$("#pnlNaoSeguro").offset({top:36, left:position.left-91});
		
		//substitui o texto "Ja é Cadastrado, clique aqui" por "Login" no header
		var loginmsg = $(".loginmsg em a").clone();
		$(".loginmsg em").text("");
		$(".loginmsg em").append(loginmsg);
		$(".loginmsg em a").text( $(".loginmsg em a").text().replace("clique aqui","Login") );
        
        
        if ( $('.MeusPedidosDetalhe .dados-entrega').size() > 1 ){
        
            $('.MeusPedidosDetalhe .dados-entrega:last').hide();
            $('.MeusPedidosDetalhe .descricao-entrega:last').hide();
            $('.MeusPedidosDetalhe .descricao-compra:last').hide();
        
        }
		
		
	});

    var deviceAgent = navigator.userAgent.toLowerCase();
	var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
	if (agentID) {
 
        if( $(window).height() < $('#general').height() ){
           
            $('#footer').css({ 'position' : 'static' });
        
        }
	}
    
    if( $('body').is('.MeusPedidosDetalhe') ){
    
        Cufon.replace('.MeusPedidosDetalhe .informacoes-entrega ul li strong', {
            fontFamily: 'Proxima Nova Rg'
        });
        Cufon.replace('.MeusPedidosDetalhe h3', {
            fontFamily: 'Proxima Nova Rg'
        });
        Cufon.replace('.MeusPedidosDetalhe h4', {
            fontFamily: 'Proxima Nova Rg'
        });
        Cufon.replace('.MeusPedidosDetalhe .endereco-entrega dl dt', {
            fontFamily: 'Proxima Nova Rg'
        });
        Cufon.replace('.MeusPedidosDetalhe .dados-entrega dl dt', {
            fontFamily: 'Proxima Nova Rg'
        });
    
        Cufon.replace('.MeusPedidosDetalhe .descricao-compra .subtotal', {
            fontFamily: 'Proxima Nova Rg'
        });
        Cufon.replace('.MeusPedidosDetalhe .descricao-compra .frete', {
            fontFamily: 'Proxima Nova Rg'
        });
        Cufon.replace('.MeusPedidosDetalhe .descricao-compra tfoot .valor-total', {
            fontFamily: 'Proxima Nova Rg'
        });
        Cufon.replace('.box-dados-pagamento .valor-compra th', {
            fontFamily: 'Proxima Nova Rg'
        });
        
 
    } 
    
});
	
$(document).ready(function () {
    
    
	var delayfix = setTimeout(menufix,3000);

	var loginLink = setTimeout(function(){$('#lblLoginMsg').wrap("<a href='/Vitrine/minhaconta/minhaconta.aspx' />")}, 2000);
	
	var loginLink = setTimeout(function(){
		if($(".loginmsg").length == 1){
			$("#product #likeitbox #resenha #rtAvaliacao_Star_5").css({
				"background": "url('/Control/ArquivoExibir.aspx?NomeArquivo=img_loveit_login_hover.png') no-repeat scroll 0 0 transparent",
				"left": "50%",
				"margin-left": "-70px",
				"top": "-60px",
				"width": "140px"
			});
			$("#product #likeitbox #resenha #rtAvaliacao_A").attr({onmouseout: "", href: "/Site/Login.aspx"});
		}
	}, 2000);
    
    
    var printheader = setTimeout(function(){

        $('#header .wrapper').fadeTo(300, 1);
          
	}, 3500);
    

    $(".share").each(function(){
        $(this).find("li").each(function(){
        
            var link = $(this).find(" > span").attr("class");
            var url = location.pathname;
            var network = '';
            var action = '';
            
            if(link == "st_facebook_large"){
                network = 'facebook';
                action = 'share';
            }
            if(link == "st_twitter_large"){
                network = 'twitter';
                action = 'share';
            }
            if(link == "st_email_large"){
                network = 'email';
                action = 'send';
            }
            $(this).bind("click", function(){
                _gaq.push(['_trackSocial', network, action, url]); 
            });
        });
    });
    
    if ( param('mi') != "" ){
    
        _gaq.push(['_setCustomVar ',
            5,       
            "Midia_Interna",   
            param('mi'),
            2            
        ]);

    }
    
});	


function menufix() { //

	//Corrigindo o espacamento do menu
	var size = 0;
	$('#navigation li:not(li li)').delay(2000).each(function () {
		size += ( $(this).width() + 8 );
	});

	var diference = ( parseInt( $('#navigation').width() - size ) / 2 ) ;
	$('#navigation').width( ( $('#navigation').width() - diference ) );
	$('#navigation').css({ 'padding-left' : diference + 'px'  });

}
	
 
function param( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
} 